package com.mall.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.result.Result;
import com.mall.system.entity.Logininfor;
import com.mall.system.service.LogininforService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 登录日志控制器
 */
@Tag(name = "登录日志管理")
@Slf4j
@RestController
@RequestMapping("/system/logininfors")
public class LogininforController {
    
    @Autowired
    private LogininforService logininforService;
    
    @Operation(summary = "登录日志列表")
    @GetMapping
    public Result<IPage<Logininfor>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            Logininfor logininfor) {
        try {
            IPage<Logininfor> page = new Page<>(pageNum, pageSize);
            QueryWrapper<Logininfor> queryWrapper = new QueryWrapper<>();
            
            // 添加查询条件
            if (logininfor.getUserName() != null && !logininfor.getUserName().isEmpty()) {
                queryWrapper.like("user_name", logininfor.getUserName());
            }
            if (logininfor.getStatus() != null) {
                queryWrapper.eq("status", logininfor.getStatus());
            }
            if (logininfor.getIpaddr() != null && !logininfor.getIpaddr().isEmpty()) {
                queryWrapper.like("ipaddr", logininfor.getIpaddr());
            }
            
            queryWrapper.orderByDesc("login_time");
            
            IPage<Logininfor> result = logininforService.page(page, queryWrapper);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询登录日志列表异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "根据登录日志ID获取登录日志")
    @GetMapping("/{infoId}")
    public Result<Logininfor> getById(@PathVariable("infoId") Long infoId) {
        try {
            Logininfor logininfor = logininforService.getById(infoId);
            if (logininfor != null) {
                return Result.success(logininfor);
            } else {
                return Result.failed("登录日志不存在");
            }
        } catch (Exception e) {
            log.error("查询登录日志异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "新增登录日志")
    @PostMapping
    public Result<Boolean> add(@RequestBody Logininfor logininfor) {
        try {
            boolean success = logininforService.saveLogininfor(logininfor);
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed("新增登录日志失败");
            }
        } catch (Exception e) {
            log.error("新增登录日志异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "删除登录日志")
    @DeleteMapping("/{ids}")
    public Result<Boolean> delete(@PathVariable("ids") List<Long> ids) {
        try {
            boolean success = logininforService.removeByIds(ids);
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed("删除登录日志失败");
            }
        } catch (Exception e) {
            log.error("删除登录日志异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "清空登录日志")
    @DeleteMapping("/clean")
    public Result<Boolean> clean() {
        try {
            boolean success = logininforService.remove(new QueryWrapper<>());
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed("清空登录日志失败");
            }
        } catch (Exception e) {
            log.error("清空登录日志异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
}